from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 初始化浏览器
driver = webdriver.Firefox()
wait = WebDriverWait(driver, 10)

try:
    # 访问百度
    url = 'https://www.baidu.com/'
    driver.get(url)
    driver.maximize_window()

    # 定位搜索框并输入内容
    search_box = wait.until(EC.presence_of_element_located((By.NAME, "wd")))
    search_box.send_keys("selenium python")
    
    # 组合键操作：全选→复制→粘贴
    search_box.send_keys(Keys.CONTROL + 'a')
    search_box.send_keys(Keys.CONTROL + 'c')
    search_box.send_keys(Keys.CONTROL + 'v')

    # 提交搜索
    search_box.send_keys(Keys.RETURN)

    # 显式等待结果加载
    wait.until(EC.title_contains("selenium python"))

except Exception as e:
    print(f"执行出错: {str(e)}")
